package com.web.surf.sample.eshop.faces.backing.validators;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import com.web.surf.sample.eshop.faces.core.AbstractValidator;
import com.web.surf.sample.eshop.faces.managed.beans.AddressBean;


@ManagedBean
@RequestScoped
public class FormValidator extends AbstractValidator{

	private static String nameError="";
	private static String countryError="";
	private static String cityError="";
	private static String streetError="";
	private static String houseError="";
	private static String buildingError="";
	private static String flatError="";
	private static String zipError="";
	private static String phoneError="";
	private static String addressError="";
	
	

//	private static final String NAME_ERROR = 
//		MessageProvider.getValue("nameError", "msgs");
//	private static final String COUNTRY_ERROR = 
//		MessageProvider.getValue("addressError", "msgs");
//	private static final String ZIP_ERROR = 
//		MessageProvider.getValue("zipError", "msgs");
//	private static final String PHONE_ERROR = 
//		MessageProvider.getValue("phoneError", "msgs");
//	private static final String NUMBER_ERROR = 
//		MessageProvider.getValue("numberError", "msgs");
//	private static final String ADDRESS_ERROR = 
//		MessageProvider.getValue("addressError", "msgs");
	

//	public static void validateName(FacesContext context, UIComponent component,
//			Object value) {
//		String sValue = (String) value;		
//		if (!sValue.matches("[A-Z]{1}[A-Za-z]+")) {
//			nameError = NAME_ERROR;
//			throw new ValidatorException(new FacesMessage(nameError));
//		} else
//			nameError = "";
//	}
	
	public static void validateName(FacesContext context, UIComponent component,
			Object value) {
		if (!validateName(value)) {
			nameError = ValidationErrors.NAME_ERROR.getErrorText();
			throw new ValidatorException(new FacesMessage(nameError));
		}
		else
			nameError = "";
	}
	
	public static void validateCountry(FacesContext context, UIComponent component,
			Object value) {
		if (!validateName(value)) {
			countryError = ValidationErrors.NAME_ERROR.getErrorText();
			throw new ValidatorException(new FacesMessage(countryError));
		}
		else
			countryError = "";
	}
	
	public static void validateCity(FacesContext context, UIComponent component,
			Object value) {
		if (!validateName(value)) {
			cityError = ValidationErrors.NAME_ERROR.getErrorText();
			throw new ValidatorException(new FacesMessage(cityError));
		}
		else
			cityError = "";
	}
	
	public static void validateStreet(FacesContext context, UIComponent component,
			Object value) {
		if (!validateName(value)) {
			streetError = ValidationErrors.NAME_ERROR.getErrorText();
			throw new ValidatorException(new FacesMessage(streetError));
		}
		else
			streetError = "";
	}
	
	public static void validateHouseNumber(FacesContext context, UIComponent component,
			Object value) {		
		if (!validateNumber(value)) {
			houseError = ValidationErrors.NUMBER_ERROR.getErrorText();
			throw new ValidatorException(new FacesMessage(houseError));
		} else
			houseError = "";
	}
	
	public static void validateBuildingNumber(FacesContext context, UIComponent component,
			Object value) {		
		if (!validateNumberOrNull(value)) {
			buildingError = ValidationErrors.NUMBER_ERROR.getErrorText();
			throw new ValidatorException(new FacesMessage(buildingError));
		} else
			buildingError = "";
	}
	
	public static void validateFlatNumber(FacesContext context, UIComponent component,
			Object value) {		
		if (!validateNumber(value)) {
			flatError = ValidationErrors.NUMBER_ERROR.getErrorText();
			throw new ValidatorException(new FacesMessage(flatError));
		} else
			flatError = "";
	}	

	public static void validateZip(FacesContext context, UIComponent component,
			Object value) {		
		if ((!validateZip(value))&&(!((String)value).isEmpty())) {
			zipError = ValidationErrors.ZIP_ERROR.getErrorText();
			throw new ValidatorException(new FacesMessage(zipError));
		} else
			zipError = "";
	}

	public static void validatePhone(FacesContext context, UIComponent component,
			Object value) {		
		if (!validatePhone(value)) {
			phoneError = ValidationErrors.PHONE_ERROR.getErrorText();
			throw new ValidatorException(new FacesMessage(phoneError));
		} else
			phoneError = "";
	}
	
	public static boolean validateAddressBean(AddressBean address) {
		try {
			validateName(null, null, address.getCustomerName());
			if (address.getCountry().isEmpty()) {
				address.setCountry("Belorussia");
			}
			validateCountry(null, null, address.getCountry());
			validateCity(null, null, address.getCity());
			validateStreet(null, null, address.getStreetName());
			if (address.getStreetKind().isEmpty()) {
				address.setStreetKind("street");
			}
			validateHouseNumber(null, null, address.getHouse());			
			validateBuildingNumber(null, null, address.getBuilding());
			if (address.getBuilding().isEmpty()) {
				address.setBuilding("1");
			}
			validateFlatNumber(null, null, address.getFlat());
			validatePhone(null, null, address.getContactPhone());
			validateZip(null, null, address.getZipCode());
			addressError="";
			return true;
		} catch (ValidatorException v) {
			addressError=ValidationErrors.ADDRESS_ERROR.getErrorText();
			return false;
		}			
	}
	
	public String getAddressError() {
		return addressError;
	}

	public String getNameError() {
		return nameError;
	}

	public String getZipError() {
		return zipError;
	}

	public String getPhoneError() {
		return phoneError;
	}

	public String getCountryError() {
		return countryError;
	}

	public String getCityError() {
		return cityError;
	}

	public String getStreetError() {
		return streetError;
	}

	public String getBuildingError() {
		return buildingError;
	}	
	public String getHouseError() {
		return houseError;
	}
	
	public String getFlatError() {
		return flatError;
	}	
	
}
